// pagesA/waiters/waiters.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
import {parseTime} from '../../utils/util'
var qqmapsdk;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: '/static/images/personal/index/avatar.png',
    addresconter: '',
    address: '',
    list: [],
    pageData: {
      pageNo: 1,
      pageSize: 10
    },
    isLoading:false,
    total:0,
    isLock: null,
    isToken:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    qqmapsdk = new QQMapWX({
      key: app.globalData.key
    })
    if (wx.getStorageSync('addresconter') && wx.getStorageSync('address')) {
      this.setData({
        addresconter: wx.getStorageSync('addresconter'),
        address: wx.getStorageSync('address')
      })
    } else {
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          qqmapsdk.reverseGeocoder({
            location: {
              latitude: latitude,
              longitude: longitude
            },
            success: (res => {
              let resultData = res.result.address_component
              let addresconter = resultData.province + ' ' + resultData.city + ' ' + resultData.district
              wx.setStorageSync('addresconter', addresconter)
              let address = {
                province: resultData.province,
                city: resultData.city,
                areas: resultData.district
              }
              wx.setStorageSync('address', address)
            })
          })
        }
      })
    }
    if(wx.getStorageSync('token')){
      this.getList()
      this.setData({
        isToken:true
      })
    }else{
      this.setData({
        isToken:false
      })
    }
  },
  getList() {
    let that = this
    that.setData({
      isLoading:true
    })
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    wx.request({
      url: app.globalData.serverUrl + '/customer/page/' + that.data.pageData.pageNo + '/' + that.data.pageData.pageSize,
      method: 'GET',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (res => {
        let resData = res.data
        if (resData.code == 200) {
          resData.data.ddOrderPage.records.map(item=>{
            item.createTime = parseTime(item.createTime,'{m}月{d}日')
            item.deviceReturnTime = parseTime(item.deviceReturnTime,'{m}月{d}日')
          })
          // that.setData({
          //   list:resData.data.ddOrderPage.records,
          //   total:resData.data.ddOrderPage.total
          // })
          let arr = that.data.list.concat(resData.data.ddOrderPage.records)
          that.setData({
            list: arr,
            total:resData.data.ddOrderPage.total
          })
          wx.hideLoading()
        }
      }),
      complete(){
        that.setData({
          isLoading:false
        })
      }
    })
  },
  heldLink() {
    wx.redirectTo({
      url: '/pagesA/waiters/waiters',
    })
  },
  heldLinkDetail(e) {
    let that = this
    let lock=null
    if(!that.data.isLock){
      that.setData({
        isLock:new Date().getTime()
      })
    }else{
      lock = new Date().getTime()
    }
    if(that.data.isLock && lock){
      if(lock-that.data.isLock<app.globalData.isNumLock){
        return
      }else{
        that.setData({
          isLock:lock
        })
      }
    }
    wx.navigateTo({
      url: '/pagesA/orderswaiters/orderswaiters?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      isLock: null
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 1. 重置关键数据
    this.setData({
      'pageData.pageNo': 1,
      total: 0,
      isloading: false,
      list:[]
    })
    // 2. 重新发起请求 并关闭下拉窗口
    this.getList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  
  onReachBottom() {
    let that = this
    if(that.data.isLoading)return
    let pageNo =that.data.pageData.pageNo
    this.setData({
      'pageData.pageNo': pageNo += 1 // 让页码值自增 +1
    })
    if(that.data.list.length<that.data.total){
      that.getList() // 重新获取列表数据
    }else{
         wx.showToast({
           title: '暂无更多数据！',
           icon:'none'
         })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})